package org.example.controller;

import jakarta.servlet.http.HttpServletResponse;
import lombok.SneakyThrows;
import org.example.bean.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.PrintWriter;
import java.util.Arrays;
import java.util.List;

@Controller
@RequestMapping("/second")
public class SecondController {
    @SneakyThrows//抛出异常
    @RequestMapping("/a")
    public void a(HttpServletResponse response) {
        System.out.println("输出到控制台");
        PrintWriter out = response.getWriter();
        out.print("hello world");
    }
    // 作用是：返回值为内容输出，而不是页面跳转
    @ResponseBody
    @RequestMapping("/b")
    public String b(){
        return "hello world!2";
    }
    @ResponseBody
    @RequestMapping("/c")
    public int c(){
        return 19;
    }
    @ResponseBody
    @RequestMapping("/d")
    public List<String> d(){
        return Arrays.asList("aa","bb","cc","dd");
    }
    @ResponseBody
    @RequestMapping("/e")
    public User e(){
        User user = new User();
        user.setName("gg");
        user.setAge(56);
        user.setLoves(new String[]{"aa","bb","cc"});
        return user;
    }
}
